/*
 * Copyright 2000-2020 ALPHA LAB.Inc All Rights Reserved.
 */
package com.alphalab.framework.context;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; 
import com.alphalab.framework.domain.AbstractValueObject;
import com.alphalab.framework.domain.ItemCondition;
import com.alphalab.framework.domain.dto.ListResultVO;
import com.alphalab.framework.domain.dto.SingleResultVO;
import com.alphalab.framework.log.IMoLog;
import com.alphalab.framework.log.MoLogFactory;
import com.alphalab.framework.role.IGeneralQueryRole;

/**
 * 应对各种场景下的业务处理，属于服务交互层.
 * 主要提供面向客户端的接口服务.
 * @param <VO>
 * @param <ROLE>
 * @author allen 
 * @version 1.0.0 2017年8月15日
 */
public class GeneralQueryContext<VO extends AbstractValueObject, ROLE extends IGeneralQueryRole<VO>>
	implements IGeneralQueryContext<VO>  {
	
	/**
	 * logger.
	 */
	private static final IMoLog logger = MoLogFactory.getLog(GeneralQueryContext.class);

	/**
	 * generalQueryRole.
	 */
	private ROLE generalQueryRole;

	/**
	 * 设置generalQueryRole.
	 * @param newgeneralQueryRole newgeneralQueryRole
	 */
	public void setGeneralQueryRole(final ROLE newgeneralQueryRole) {
		this.generalQueryRole = newgeneralQueryRole;
	}

	/**
	 * 获取generalQueryRole.
	 * @return ROLE
	 */
	public ROLE getGeneralQueryRole() {
		return this.generalQueryRole;
	}

	/**
	 * 根据主键返回对象.
	 * @param id 主键
	 * @return VO
	 */
	@RequestMapping("/find")
	@ResponseBody
	@Override
	public SingleResultVO<VO> find(final Serializable id) {
		VO vo = null;
		SingleResultVO<VO> rstVo = new SingleResultVO<VO>();
		try {
			vo = getGeneralQueryRole().find(id);
			rstVo = new SingleResultVO(vo);
		} catch (Exception ex) {
			logger.error(ex);
			rstVo = new SingleResultVO(ex); 
		}
		return rstVo;
	}

	/**
	 * 根据查询条件返回分页结果集.
	 * @param itemConditions 查询条件，为空则表示无条件
	 * @param pageIndex 第几页，从1开始
	 * @param pageSize 每页的记录总数
	 * @param orderBy 排序字段，为空表示默认排序
	 * @param isDesc 是否降序，为true表示降序
	 * @return List<VO>
	 */
	@RequestMapping("/findListByPageWithCondition")
	@ResponseBody
	@Override
	public ListResultVO<VO> findListByPageWithCondition(final List<ItemCondition> itemConditions, 
			final int pageIndex, final int pageSize, final String orderBy, final boolean isDesc) {
		List<VO> voList =  new ArrayList<VO>();
		ListResultVO<VO> rstList = new ListResultVO<VO>();
		try {
			voList = getGeneralQueryRole().findListByPageWithCondition(itemConditions, pageIndex, 
					pageSize, orderBy, isDesc);
			rstList = new ListResultVO<VO>(voList);
		} catch (Exception ex) {
			logger.error(ex);
			rstList = new ListResultVO<VO>(ex);
		}
		return rstList;
	}

	/**
	 * 根据查询条件返回全部结果集.
	 * @param itemConditions 查询条件，为空则表示无条件
	 * @param orderBy 排序字段，为空表示默认排序
	 * @param isAsc 是否升序，为1表示升序
	 * @return List<VO>
	 */
	@RequestMapping("/findListWithCondition")
	@ResponseBody
	@Override
	public ListResultVO<VO> findListWithCondition(final List<ItemCondition> itemConditions, 
			final String orderBy, final boolean isAsc) {
		List<VO> voList =  new ArrayList<VO>();
		ListResultVO<VO> rstList = new ListResultVO<VO>();
		try {
			voList = getGeneralQueryRole().findListWithCondition(itemConditions, orderBy, isAsc);
			rstList = new ListResultVO<VO>(voList);
		} catch (Exception ex) {
			logger.error(ex);
			rstList = new ListResultVO<VO>(ex);
		}
		return rstList;
	} 

}
